<template>
  <el-row :gutter="10">
    <el-button icon="el-icon-refresh" @click="getList()">刷新</el-button>

    <el-card v-show="isShow">
      <el-col :span="12">
        <el-card>
         <img :src="require('./img/inputcar/' + url)" class="image" />
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-form ref="form" :model="form" :rules="rules"></el-form>
        <el-card>
          <el-card>车牌号：{{payform.license}} </el-card>
          <el-card>汽车类型：{{payform.cartype}} </el-card>
          <el-card>出库时间：{{payform.outtime}} </el-card>
          <el-card>应缴费用：{{payform.payment}} </el-card>
        </el-card>

        <el-card>
          <el-button type="success" @click="submitForm()">确认支付</el-button>
        </el-card>
      </el-col>
    </el-card>

  </el-row>

</template>

<script>
  import {
    listPrecords,
    getPrecords,
    delPrecords,
    addPrecords,
    updatePrecords
  } from "@/api/system/precords";

  export default {
    name: "Precords",
    data() {
      return {
        url : null,
        //定时器
        timer: null,
        isShow: false,
        //缴费信息
        payform: {
          license: null,
          outtime: null,
          cartype: null,
          payment: null,
          enterpicpath: null,
        },
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 车辆信息表格数据
        precordsList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          license: null,
          entertime: null,
          cartype: null,
        },
        options: [{
          value: '小车',
          label: '小车'
        }, {
          value: '大车',
          label: '大车'
        }, {
          value: '新能源车',
          label: '新能源车'
        }, ],
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
    },
    mounted() {
      this.refresh();
    },
    methods: {
      refresh(){
        if(!this.isShow){
          this.timer = setInterval(() => {
            setTimeout(this.getList(), 0)
          }, 1000*5)
        }
      },
      /** 查询车辆信息列表 */
      getList() {
        this.loading = true;
        this.isShow = false;
        listPrecords(this.queryParams).then(response => {
          this.precordsList = response.rows;
          this.total = response.total;
          this.loading = false;
          console.info(response.rows[0].outtime);

          var i = 0;
          while (this.precordsList[i] != null) {
            if (this.precordsList[i].outtime != null && this.precordsList[i].ispay == 0) {
              this.payform.license = this.precordsList[i].license;
              this.payform.cartype = this.precordsList[i].cartype;
              this.payform.outtime = this.precordsList[i].outtime;
              this.payform.payment = this.precordsList[i].payment;
              this.payform.enterpicpath = this.precordsList[i].enterpicpath;
              this.form = this.precordsList[i];
              this.isShow = true;
            }
            i = i + 1;
          }
          this.url = this.payform.license+".jpg"
          console.info(this.url);
        });

      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          pid: null,
          license: null,
          entertime: null,
          outTime: null,
          cartype: null,
          enterPicPath: null,
          outPicPath: null,
          isPay: null,
          deleted: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },

      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.queryParams.license = null;
        this.queryParams.entertime = null;
        this.queryParams.cartype = null;
        this.handleQuery();
      },

      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.pid)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加车辆信息";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const pid = row.pid || this.ids
        getPrecords(pid).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改车辆信息";
        });
      },
     /** 提交按钮 */
           submitForm() {
             this.$refs["form"].validate(valid => {
               if (valid) {
                 // 更新出库时间和支付状态
                 this.form.outtime = this.payform.outtime; // 确保出库时间被设置
                 this.form.ispay = 1; // 标记为已支付
           
                 if (this.form.pid != null) {
                   updatePrecords(this.form).then(response => {
                     this.isShow = false;
                     this.$modal.msgSuccess("支付成功");
                     this.open = false;
                     this.getList(); // 重新加载列表以更新状态
                   });
                 } else {
                   addPrecords(this.form).then(response => {
                     this.$modal.msgSuccess("支付成功");
                     this.open = false;
                     this.getList(); // 重新加载列表以更新状态
                   });
                 }
               }
             });
           },
      /** 删除按钮操作 */
      handleDelete(row) {
        const pids = row.pid || this.ids;
        this.$modal.confirm('是否确认删除车辆信息编号为"' + pids + '"的数据项？').then(function() {
          return delPrecords(pids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/precords/export', {
          ...this.queryParams
        }, `precords_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>


<style scoped>
  .image {
    width: 600px;
    height: 480px;
  }
</style>
